<?php

namespace app\api\controller;


use addons\epay\library\Service;
use app\api\model\Auction;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Callback extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 开启
     *
     */
    public function OpenAuction()
    {
        $auction_id = input('auction_id','');
        $auction = new Auction();
        $res = $auction->cronOpenAuction($auction_id);
        $this->success('请求成功',$res);
    }

    /**
     * 关闭
     *
     */
    public function CloseAuction()
    {
        try {
            $auction_id = input('auction_id','');
            $auction = new Auction();
            $auction_info = Auction::get($auction_id);
            if (!$auction_info) $this->error("拍卖活动不存在");

            $res = $auction->invalidAuction($auction_id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);

    }

    //尾款支付回调
    public function finalnotify()
    {
        $paytype = "wechat";
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        $out_trade_no = $data['out_trade_no'];
        $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;

        $model = new \app\api\model\AuctionOrder();

        $res = $model->finalnotify($out_trade_no);

        $this->success('提交成功',$res);
    }

    //定金支付回调
    public function notify()
    {
        $paytype = "wechat";
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        $out_trade_no = $data['out_trade_no'];
        $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;

        $model = new \app\api\model\AuctionOrder();

        $res = $model->notify($out_trade_no);

        $this->success('提交成功',$res);
    }
}